package com.hexu.event.view;

import com.hexu.event.bean.UserBean;
import com.hexu.event.entity.User;
import com.hexu.event.util.StageUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.hexu.event.util.ServiceUtil.userService;
import static com.hexu.event.util.StageUtil.display;

public class UserTableView {
    //存被选择的表格信息
    static String selectPhoneNumber = null;
    static Integer selectIndex = null;
    static String selectFreeze = null;
    public static String selectDeblocking = null;
    static String selectUserName = null;
    static String selectSex = null;
    static Integer selectBalance = null;
    //存解封时间
    public static String freezeDate;
    //存被冻结的手机号码
    public static String[] freezePhoneNumber;
    //存冻结手机号码的数量

    static TextField textFieldHour = new TextField();
    static TextField textFieldMinute = new TextField();
    public void viewUserInformation() throws ParseException {
        //传入用户信息
        List<User> userList = userService.getUserInformation();
        //传入用户数量
        int userNumber = userService.userNumber();

        List<UserBean> userBeanList = new ArrayList<>();

        TableView<UserBean> table = new TableView<>();

        for(int i = 0;i<userNumber;i++){
            //添加用户信息
            userBeanList.add(  new UserBean(userList.get(i).getPhoneNumber(),userList.get(i).getUserName(),
                    userList.get(i).getSystemBalance(),userList.get(i).getSex(),userList.get(i).getFreeze(),userList.get(i).getDeblocking())
            );
        }
        //转化成表可存放的数据
        ObservableList<UserBean> data =
                FXCollections.observableArrayList(
                        userBeanList
                );


        //用户名字
        //列的数据类型
        TableColumn<UserBean,String> nameCol =
                new TableColumn<>("用户姓名");
        //设置出是最小宽度
        nameCol.setMinWidth(100);
        nameCol.setCellValueFactory(
                new PropertyValueFactory<>("userName"));
        //用户手机号码
        TableColumn<UserBean, String> phoneNumberCol =
                new TableColumn<>("手机号码");
        phoneNumberCol.setMinWidth(200);
        phoneNumberCol.setCellValueFactory(
                new PropertyValueFactory<>("phoneNumber"));
        //账户余额
        TableColumn<UserBean, Integer> balanceCol = new TableColumn<>("账号余额");
        balanceCol.setMinWidth(75);
        balanceCol.setCellValueFactory(
                new PropertyValueFactory<>("balance"));
        //性别
        TableColumn<UserBean,String> sexCol = new TableColumn<>("性别");
        sexCol.setMinWidth(75);
        sexCol.setCellValueFactory(
                new PropertyValueFactory<>("sex"));

        //状态
        TableColumn<UserBean,String> freezeCol = new TableColumn<>("状态");
        freezeCol.setMinWidth(75);
        freezeCol.setCellValueFactory(
                new PropertyValueFactory<>("freeze"));
        //解封时间
        TableColumn<UserBean,String> deblockingCol = new TableColumn<>("解封时间");
        deblockingCol.setMinWidth(150);
        deblockingCol.setCellValueFactory(
                new PropertyValueFactory<>("deblocking"));

        //接收信息
        table.setItems(data);
        //把数据放在相应列里
        table.getColumns().addAll(nameCol, phoneNumberCol,balanceCol,sexCol,freezeCol,deblockingCol);

        Button freeze = new Button("冻结");
        Button deblocking = new Button("解封");
        Label labelHour = new Label("小时");
        Label labelMinute = new Label("分钟");

        //限制只能输入数字
        StageUtil.limit(textFieldHour);
        StageUtil.limit(textFieldMinute);
        textFieldHour.setText("0");
        textFieldMinute.setText("0");

        HBox hBox = new HBox();
        hBox.setSpacing(10);
        hBox.getChildren().addAll(textFieldHour,labelHour,textFieldMinute,labelMinute,freeze);

        VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(table,deblocking,hBox);

        StageUtil.showStage(new Stage(),new Scene(vbox),550,700,"用户信息",false);

        //设置点击事件
        table.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //获取选择景点的序列
                selectIndex = table.getSelectionModel().getSelectedIndex();
                //如果点击的时候不是点击空处
                if(selectIndex != -1){
                    //获取选择景点的信息
                    selectPhoneNumber = table.getSelectionModel().getSelectedItems().get(0).getPhoneNumber();
                    selectFreeze = table.getSelectionModel().getSelectedItems().get(0).getFreeze();
                    selectDeblocking = table.getSelectionModel().getSelectedItems().get(0).getDeblocking();
                    selectUserName = table.getSelectionModel().getSelectedItems().get(0).getUserName();
                    selectSex = table.getSelectionModel().getSelectedItems().get(0).getSex();
                    selectBalance = table.getSelectionModel().getSelectedItems().get(0).getBalance();

                }
            }
        });

        //设置 冻结 单击事件
        freeze.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    freeze(table);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
        //设置 解封 单击事件
        deblocking.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                deblocking(table);
            }
        });

    }

    //冻结
    public static void freeze(TableView table) throws ParseException {
        //如果选择到了用户
        if(selectIndex != null && selectIndex != -1){
            User user = new User();
            user.setPhoneNumber(selectPhoneNumber);
            user.setDeblocking(getDeblockingTime(Integer.valueOf(textFieldHour.getText()),Integer.valueOf(textFieldMinute.getText())));
            user.setFreeze("冻结");

            Boolean success = userService.freeze(user);
            display(success,new Stage(),"冻结");

            if(success){
                //修改表的内容
                table.getItems().set(selectIndex,new UserBean(selectPhoneNumber,
                        selectUserName,
                        selectBalance,
                        selectSex,
                        "冻结",
                        user.getDeblocking()));
                freezeDate = user.getDeblocking();
                new TimerTest02();

            }
        }

    }

    //解封
    public static void deblocking(TableView table){
        //如果选择到了用户
        if(selectIndex != null && selectIndex != -1){
            User user = new User();
            user.setPhoneNumber(selectPhoneNumber);
            user.setFreeze("解封");
            user.setDeblocking("无");
            Boolean success = userService.freeze(user);
            display(success,new Stage(),"解封");

            if(success){
                //修改表的内容
                table.getItems().set(selectIndex,new UserBean(selectPhoneNumber,
                        selectUserName,
                        selectBalance,
                        selectSex,
                        "解封",
                        "无"));
            }
        }

    }

    //获取冻结后的解封时间
    public static String getDeblockingTime(int hours,int minutes){
        long time = System.currentTimeMillis();
        time += minutes*60*1000+hours*60*60*1000;

        Date date = new Date(time);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    //比较时间，是否解封
    public static boolean compareTime(String deblocking) throws ParseException {
        if(deblocking.contains("无")){
            return true;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = dateFormat.parse(deblocking);
        String currentTime = dateFormat.format(new Date());
        Date date2 = dateFormat.parse(currentTime);

        //此时解封
        if(date1.compareTo(date2) <= 0){

            return true;
        }else
            return false;

    }
}

